package cn.iocoder.yudao.module.member.controller.app.user;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.member.controller.app.user.vo.*;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.io.IOException;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户个人中心")
@RestController
@RequestMapping("/member/user")
@Validated
@Slf4j
public class AppMemberUserController {

    @Resource
    private MemberUserService userService;
    @Resource
    private MemberLevelService levelService;

    @Resource
    private FileApi fileApi;

    @PutMapping(name = "/uploadAvatarImg", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperationSupport(order = 1)
    @Operation(summary = "1.上传个人头像", security = { @SecurityRequirement(name = "Authorization") })
    public CommonResult<Boolean> uploadAvatarImg(@RequestPart("avatarImgFile") MultipartFile avatarImgFile)  throws IOException {
        byte[] content = IoUtil.readBytes(avatarImgFile.getInputStream());
        String imgUrl = fileApi.createFile(content, avatarImgFile.getOriginalFilename(), null, avatarImgFile.getContentType());
        userService.updateUser(getLoginUserId(), new AppMemberUserUpdateReqVO().setAvatarUrl(imgUrl));
        return success(true);
    }

    @PutMapping("/update")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "2.修改个人基本信息（包括上传头像）", security = { @SecurityRequirement(name = "Authorization") })
    public CommonResult<Boolean> updateUser(
            @RequestParam(name="nickname", required = false, defaultValue = "李四") String nickname,
            @RequestPart(name="avatarImgFile", required = false)  MultipartFile avatarImgFile,
            @RequestParam(name="gender", required = false, defaultValue = "1") Integer gender,
            @RequestParam(name="userMood", required = false, defaultValue = "test@163.com") String userMood
    ) throws IOException {
        byte[] content = IoUtil.readBytes(avatarImgFile.getInputStream());
        String imgUrl = fileApi.createFile(content, avatarImgFile.getOriginalFilename(), null, avatarImgFile.getContentType());
        userService.updateUser(getLoginUserId(),
                new AppMemberUserUpdateReqVO()
                        .setNickname(nickname).setAvatarUrl(imgUrl).setGender(gender).setUserMood(userMood)

        );
        return success(true);
    }

    @PutMapping("/reset-password")
    @Operation(summary = "3.重置密码", description = "用户忘记密码时使用", security = { @SecurityRequirement(name = "Authorization") })
    @ApiOperationSupport(order = 3)
    @PermitAll
    public CommonResult<Boolean> resetUserPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
        userService.resetUserPassword(reqVO);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "4.获得个人基本信息", security = { @SecurityRequirement(name = "Authorization") })
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo() {
        MemberUserDO user = userService.getUser(getLoginUserId());
        MemberLevelDO level = levelService.getLevel(user.getLevelId());
        return success(MemberUserConvert.INSTANCE.convert(user, level));
    }

    @PutMapping("/update2")
    @ApiOperationSupport(order = 10)
    @Deprecated
    @Operation(summary = "10.修改个人基本信息（头像图片使用URL地址）", security = { @SecurityRequirement(name = "Authorization", scopes = "bearer test1")  })
    public CommonResult<Boolean> updateUser2(@RequestBody @Valid AppMemberUserUpdateReqVO reqVO) {
        userService.updateUser(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile")
    @Operation(summary = "修改用户手机")
    @Hidden
    public CommonResult<Boolean> updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile-by-weixin")
    @Operation(summary = "基于微信小程序的授权码，修改用户手机")
    @Hidden
    public CommonResult<Boolean> updateUserMobileByWeixin(@RequestBody @Valid AppMemberUserUpdateMobileByWeixinReqVO reqVO) {
        userService.updateUserMobileByWeixin(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(summary = "修改用户密码", description = "用户修改密码时使用")
    @Hidden
    public CommonResult<Boolean> updateUserPassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }



}